package com.txc.pay.pay.impl;

import cn.hutool.json.JSONUtil;
import com.txc.pay.entity.AccountEntity;
import com.txc.pay.vo.OrderVo;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.payments.jsapi.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import org.springframework.stereotype.Service;

/**
 * @ClassName: WxJsapiPay
 * @Description: 微信jsapi支付实现
 * @Author: TXC
 * @Date: 2023-12-26 11:34
 **/
@Service
public class WxJsapiPay extends WxPay {

    @Override
    public String pay(OrderVo order) {
        AccountEntity account = getAccount(order.getAccountId());
        // 构建service
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig(order.getAccountId())).build();
        // request.setXxx(val)设置所需参数，具体参数可见Request定义
        PrepayRequest request = new PrepayRequest();
        request.setAppid(wxPayConfig(account).getAppId());
        request.setMchid(wxPayConfig(account).getMerchantId());
        request.setDescription(order.getTitle());
        request.setOutTradeNo(order.getId());
        request.setNotifyUrl(wxPayConfig(account).getPayNotifyUrl());
        Amount amount = new Amount();
        amount.setTotal(Integer.valueOf(order.getPriceFen()));
        request.setAmount(amount);
        Payer payer = new Payer();
        payer.setOpenid(order.getUserId());
        request.setPayer(payer);
        // 调用预下单接口
        return JSONUtil.parse(service.prepayWithRequestPayment(request)).toStringPretty();

    }

    @Override
    public String queryOrderById(String transactionId, String accountId) {
        AccountEntity account = getAccount(accountId);
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig(accountId)).build();
        QueryOrderByIdRequest request = new QueryOrderByIdRequest();
        request.setTransactionId(transactionId);
        request.setMchid(wxPayConfig(account).getMerchantId());
        Transaction transaction =service.queryOrderById(request);
        return transaction.toString();
    }

    @Override
    public String queryOrderByOutTradeNo(String outTradeNo, String accountId) {
        AccountEntity account = getAccount(accountId);
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig(accountId)).build();
        QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
        request.setOutTradeNo(outTradeNo);
        request.setMchid(wxPayConfig(account).getMerchantId());
        Transaction transaction =service.queryOrderByOutTradeNo(request);
        return transaction.toString();
    }

    @Override
    public String closeOrder(String outTradeNo, String accountId) {
        AccountEntity account = getAccount(accountId);
        JsapiServiceExtension service = new JsapiServiceExtension.Builder().config(rsaAutoCertificateConfig(accountId)).build();
        CloseOrderRequest request = new CloseOrderRequest();
        request.setMchid(wxPayConfig(account).getMerchantId());
        request.setOutTradeNo(outTradeNo);
        service.closeOrder(request);
        return null;
    }
}
